package Viewer;

import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;

/**
 * The Downpanel contains functionalities for searching features like the
 * {@link MotifSearch}, the {@link FeatureTable} and the
 * {@link NamePositionSearch}. The Downpanel is embedded in the {@link Viewer}.
 * 
 * @author Stefan Lorenz<br>
 * <br>
 *         26.11.2012
 */
@SuppressWarnings("serial")
public class DownPanel extends JTabbedPane {

	/**
	 * Browsing frame of GeneScapes
	 */
	private Viewer viewer;
	/**
	 * Panel for searching IUPAC nucleotide motifs
	 */
	private MotifSearch motifSearch;
	/**
	 * Panel to search for genes by ID or position
	 */
	private NamePositionSearch namePositionSearch;
	/**
	 * Table of all features
	 */
	private FeatureTable featureTable;
	/**
	 * The tabbedPane splits the searching panel in two groups
	 */
	private JTabbedPane tabbedPane;

	/**
	 * Constructor of the DownPanel
	 * 
	 * @param viewer
	 *            a GeneScapes browsing session
	 */
	public DownPanel(final Viewer viewer) {
		this.viewer = viewer;

		JPanel gotoPanel = new JPanel();
		gotoPanel.setFocusable(false);
		this.namePositionSearch = new NamePositionSearch(this.viewer);
		this.namePositionSearch.setOpaque(false);
		this.motifSearch = new MotifSearch(this.viewer);
		this.motifSearch.setOpaque(false);
		gotoPanel.setLayout(new GridLayout(2, 1));
		gotoPanel.add(this.motifSearch);
		gotoPanel.add(this.namePositionSearch);

		this.featureTable = new FeatureTable(this.viewer);
		JScrollPane scrollPane = new JScrollPane(this.featureTable);
		scrollPane.setOpaque(false);

		this.tabbedPane = new JTabbedPane();

		JPanel positionPanel = new JPanel();
		positionPanel.setLayout(new GridBagLayout());

		GridBagConstraints gc = new GridBagConstraints();
		int posx = 0;
		int posy = 0;
		for (int j = 0; j < this.viewer.getPositionOfTrack().length; j++) {
			TrackShifting tpl = new TrackShifting(j, this.viewer);
			this.viewer.setTrackPositionLines(j, tpl);
			if (j % 4 == 0) {
				posy++;
				posx = 0;
			}
			gc.gridx = posx;
			gc.gridy = posy;
			positionPanel.add(this.viewer.getTrackPositionLines(j), gc);
			posx++;
		}

		// add to tapped pane
		JScrollPane shifter = new JScrollPane(positionPanel);
		this.addTab("Search", gotoPanel);
		this.addTab("Shift & Anchor", shifter);
		this.addTab("Feature Table " + this.viewer.getLoadedTracks()[1].getFile().getName(), scrollPane);
	}

	/**
	 * Sets the name of the {@link FeatureTable}
	 * 
	 * @param s
	 *            the name of the {@link FeatureTable}
	 */
	public void setFeatureTableName(String s) {
		this.tabbedPane.setTitleAt(2, s);
	}

	/**
	 * Sets a new {@link FeatureTable}
	 * 
	 * @param featureTable
	 *            the {@link FeatureTable} set
	 */
	public void setFeatureTable(FeatureTable featureTable) {
		this.featureTable = featureTable;
	}

	/**
	 * Returns the current shown {@link FeatureTable}
	 * 
	 * @return the current shown {@link FeatureTable}
	 */
	public FeatureTable getFeatureTable() {
		return featureTable;
	}

	/**
	 * Sets the Panel for search by position and name
	 * 
	 * @param namePositionSearch
	 *            panel to search by name and position
	 */
	public void setNamePositionSearch(NamePositionSearch namePositionSearch) {
		this.namePositionSearch = namePositionSearch;
	}

	/**
	 * Returns the panel for searching by name or position
	 * 
	 * @return the position and name search panel
	 */
	public NamePositionSearch getNamePositionSearch() {
		return namePositionSearch;
	}

	/**
	 * Sets the panel of the motif search
	 * 
	 * @param motifSearch
	 */
	public void setMotifSearch(MotifSearch motifSearch) {
		this.motifSearch = motifSearch;
	}

	/**
	 * Returns the panel of the motif search
	 * 
	 * @return the panel of the motif search
	 */
	public MotifSearch getMotifSearch() {
		return motifSearch;
	}

	/**
	 * Updates the name search box
	 * 
	 * @param i
	 *            the index of the track which data should be updated
	 */
	public void updateSearchBox(int i) {
		this.namePositionSearch.updateSearchBox(i);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		// Graphics2D g2 = (Graphics2D) g;
		// g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
		// RenderingHints.VALUE_ANTIALIAS_ON);
		// g2.setRenderingHint(RenderingHints.KEY_RENDERING,
		// RenderingHints.VALUE_RENDER_SPEED);
		// g2.setColor(Color.BLACK);
		// g2.fillRect(0, 0, this.getWidth(), this.getHeight());
		// ImageIcon icon = new
		// ImageIcon(Framework.class.getResource("../resources/muster.png"));
		// AlphaComposite ac1 =
		// AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
		// g2.setComposite(ac1);
		// for (int i = 0; i < this.getWidth(); i += icon.getIconWidth()) {
		// g2.drawImage(icon.getImage(), i, (this.getHeight() / 2) -
		// icon.getIconHeight() / 2, this);
		// }
		// g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
		// 0.5f));
		// g.setFont(new Font("arial", Font.BOLD, 34));
		// g.setColor(new Color(28, 163, 138));
		// g.drawString("GENESCAPES", 30, (int) (this.getHeight() / 5));
		// g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
		// 1.0f));
	}
}
